package com.share.zabbix.method;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.share.zabbix.request.ZabbixRequest;
import com.share.zabbix.request.ZabbixRequestBuilder;
import com.share.zabbix.response.ZabbixResponseUsergroup;

public class ZabbixUserMethod extends ZabbixBaseMethod{
	/**
	 * zabbix获取用户所在的用户组
	 * 
	 * @param usergroup
	 * @return
	 */
	public static ZabbixResponseUsergroup getUsergroup(String usergroup) {
		ZabbixRequest request = ZabbixRequestBuilder.newBuilder().method("usergroup.get").build();
		request.putParam("status", 0);
		if (usergroup != null) {
			JSONObject filter = new JSONObject();
			filter.put("name", usergroup);
			request.putParam("filter", filter);
		}
		JSONObject response = call(request);
		String result = response.getJSONArray(ZABBIX_KEY_RESULT).toJSONString();
		try {
			Gson gson = new GsonBuilder().create();
			List<ZabbixResponseUsergroup> usergroupList = gson.fromJson(result,
					new TypeToken<ArrayList<ZabbixResponseUsergroup>>() {
					}.getType());
			return usergroupList.get(0);
		} catch (Exception e) {
		}
		return new ZabbixResponseUsergroup();
	}
}
